/**
 * 发送GET请求
 * 技术栈：function + ajax + callback
 * @param {String} url 请求地址 
 * @param {String} params 请求参数   格式：参数名=值&....&参数名=值
 * @param {Function} callback  回调函数
 * @param {Function} headersFn 自定义请求头
 * @returns undefined
 */


let t=null
function get(url, params, callback, setHeaderFn=null) 
{
    const xhr = new XMLHttpRequest
    xhr.onreadystatechange = () => {
        if (xhr.readyState == 4)
        {
            if(xhr.status == 200) 
            {
                let res = JSON.parse(xhr.responseText)	
                    callback(res)
            } else {
              alert(xhr.status)
            }
        }
    }
    xhr.open('get', `${url}?${params}`)
    if(setHeaderFn) setHeaderFn(xhr)
    xhr.send(null)
}
function baiDu(res){
    if(res && res.g){
        document.querySelector('ul').innerText=''
    res.g.forEach(item=>{
        let liObj=document.createElement('li')
        liObj.innerText=item.q
        document.querySelector('ul').appendChild(liObj)
        })
    } else {
        document.querySelector('ul').innerHTML=`<li>未搜索到相关数据</li>`
    }
}

function newFilms(res){
    let olObj=document.querySelector('ol')
    olObj.innerHTML=''
    if(!Array.isArray(res.data.films)) return    //多家一步过滤，防止后端数据出现误差
    res.data.films.forEach(item=>{
        let liObj=document.createElement('dl')
        liObj.innerHTML=`<dt><img src='${item.poster}' width='200' height='300'/></dt>
        <dt>${item.name}</dt> `
        olObj.appendChild(liObj)
    })
}
function setHeaderFn(xhr){
    xhr.setRequestHeader('X-Client-Info','{"a":"3000","ch":"1002","v":"5.2.0","e":"1651754242929143160045569","bc":"320100"}')
    xhr.setRequestHeader('X-Host','mall.film-ticket.film.list')
    xhr.setRequestHeader('X-Requested-With','XMLHttpRequest')
    xhr.setRequestHeader('X-Token','undefined')
}